//
//  CCArchive.swift
//  SwiftStudy
//
//  Created by chang on 2018/3/21.
//  Copyright © 2018年 chang. All rights reserved.
//

import UIKit

class CCArchive: NSObject {
    
    static let fileName = "/OneHomeInfo"
    
    //MARK: write
    class func writeArrayToFile(dataArray: NSArray) {
        let path = NSHomeDirectory().appending("/Documents/CCData")
        let isSucess = CCArchive.creatDirectories(path:path)
        if isSucess == true {
            print(path.appending(fileName))
            let isSuce = dataArray.write(toFile: path.appending(fileName), atomically: true)
            if isSuce == true {
                print("存储数据成功")
            }else{
                print("存储数据失败")
            }
        }else{
            print("创建目录失败")
        }
    }
    
    //MARK: get
    class func readArrayFromFile() ->Any {
        let path = NSHomeDirectory().appending("/Documents/CCData")
        let aFileManager = FileManager.default
        if aFileManager.fileExists(atPath: path) {
            let dataArray = NSArray.init(contentsOfFile: path.appending(fileName))
            return dataArray ?? "无"
        }else{
            print("文件目录不存在")
            return "无"
        }
    }

    // creat directories
    class func creatDirectories(path: String) -> Bool {
        let aFileManager = FileManager.default
        if aFileManager.fileExists(atPath: path) {
            return true
        }else{
            let isSucess = aFileManager.createFile(atPath: path, contents: nil, attributes: nil)
            return isSucess
        }
    }
}





